package com.sunyard.redission.demos.web;

import io.lettuce.core.ClientOptions;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.RedisPassword;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceClientConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.connection.lettuce.LettucePoolingClientConfiguration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;


@Configuration()
public class RedisAutoConfiguration {

    @Value("${spring.redis.host}")
    private String host;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.password}")
    private String password;
    @Value("${spring.redis.lettuce.pool.max-active}")
    private int maxActive;
    @Value("${spring.redis.lettuce.pool.max-idle}")
    private int maxIdle;
    @Value("${spring.redis.lettuce.pool.min-idle}")
    private int minIdle;

    /**
     * 创建 RedisTemplate Bean，使用 JSON 序列化方式
     *
     * @param connectionFactory connectionFactory
     * @return RedisTemplate
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        // 创建 Redis 配置
        RedisStandaloneConfiguration config = new RedisStandaloneConfiguration();
        config.setHostName(host);
        config.setPort(port);
        config.setPassword(RedisPassword.of(password));;
        GenericObjectPoolConfig<?> poolConfig = new GenericObjectPoolConfig<>();
        poolConfig.setMaxTotal(maxActive);      // 最大连接数
        poolConfig.setMaxIdle(maxIdle);       // 最大空闲连接数
        poolConfig.setMinIdle(minIdle);       // 最小空闲连接数
        // 创建 Lettuce 客户端配置（包含连接池配置）
        LettuceClientConfiguration clientConfig = LettucePoolingClientConfiguration.builder() // 命令超时时间
                .clientOptions(ClientOptions.builder()
                        .build())
                .poolConfig(poolConfig)
                .build();
        // 创建 Lettuce 连接工厂
        LettuceConnectionFactory lettuceConnectionFactory = new LettuceConnectionFactory(config, clientConfig);
        lettuceConnectionFactory.afterPropertiesSet();
        connectionFactory = lettuceConnectionFactory;
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
        // 设置开启事务支持
        redisTemplate.setEnableTransactionSupport(true);
        // 设置 RedisConnection 工厂。
        redisTemplate.setConnectionFactory(connectionFactory);
        // 使用 String 序列化方式，序列化 KEY 。
        redisTemplate.setKeySerializer(RedisSerializer.string());
        redisTemplate.setValueSerializer(RedisSerializer.string());
        // 使用 JSON 序列化方式（库是 Jackson ），序列化 VALUE 。
        redisTemplate.setHashKeySerializer(RedisSerializer.string());
        redisTemplate.setHashValueSerializer(RedisSerializer.json());
        return redisTemplate;
    }

    @Bean
    public RedisUtils redisUtils(){
        return new RedisUtils();
    }
}